#include "motor_task.h"

static uint16_t motor_speed = 600;

void Motor_Task_OLED(void *pvParameters);
#define OLED_TASK_STACK_SIZE 128
#define OLED_TASK_PRIORITY 3
#define OLED_TASK_NAME "Motor_Task_OLED"
#define OLED_TASK_PERIOD_MS 10
TaskHandle_t OLED_Task_Handle = NULL;

// 电机驱动任务
void Motor_Task_MOTOR(void *pvParameters);
#define MOTOR_TASK_STACK_SIZE 128
#define MOTOR_TASK_PRIORITY 4
#define MOTOR_TASK_NAME "Motor_Task_MOTOR"
#define MOTOR_TASK_PERIOD_MS 15
TaskHandle_t MOTOR_Task_Handle = NULL;

// 按键任务
void Motor_Task_KEY(void *pvParameters);
#define KEY_TASK_STACK_SIZE 128
#define KEY_TASK_PRIORITY 3
#define KEY_TASK_NAME "Motor_Task_KEY"
#define KEY_TASK_PERIOD_MS 15
TaskHandle_t KEY_Task_Handle = NULL;

// 接收数据任务
void Motor_Task_RECEIVE(void *pvParameters);
#define RECEIVE_TASK_STACK_SIZE 128
#define RECEIVE_TASK_PRIORITY 5
#define RECEIVE_TASK_NAME "Motor_Task_RECEIVE"
#define RECEIVE_TASK_PERIOD_MS 15
TaskHandle_t RECEIVE_Task_Handle = NULL;
void Motor_Task_Start(void)
{

    OLED_Init();

    // 创建显示任务
    // 参数1：任务名称，
    // 参数2：任务代码，
    // 参数3：任务栈大小，
    // 参数4：任务参数，
    // 参数5：任务优先级，
    // 参数6：任务句柄
    xTaskCreate(Motor_Task_OLED, OLED_TASK_NAME, OLED_TASK_STACK_SIZE, NULL, OLED_TASK_PRIORITY, &OLED_Task_Handle);

    // 创建电机驱动任务
    xTaskCreate(Motor_Task_MOTOR, MOTOR_TASK_NAME, MOTOR_TASK_STACK_SIZE, NULL, MOTOR_TASK_PRIORITY, &MOTOR_Task_Handle);

    // 创建按键任务
    xTaskCreate(Motor_Task_KEY, KEY_TASK_NAME, KEY_TASK_STACK_SIZE, NULL, KEY_TASK_PRIORITY, &KEY_Task_Handle);

    // 创建接收数据任务
    xTaskCreate(Motor_Task_RECEIVE, RECEIVE_TASK_NAME, RECEIVE_TASK_STACK_SIZE, NULL, RECEIVE_TASK_PRIORITY, &RECEIVE_Task_Handle);
    // 开启调度器
    vTaskStartScheduler();
}

void Motor_Task_OLED(void *pvParameters)
{
    TickType_t preTick = xTaskGetTickCount();
    uint8_t buffer[20] = {0};
    while (1)
    {
        sprintf(buffer, "speed:%d", motor_speed);
        OLED_ShowString(2, 2, buffer, 1);
        // OLED_ShowString(7, 7, "1104zfx", 1);
        vTaskDelayUntil(&preTick, OLED_TASK_PERIOD_MS);
    }
}

void Motor_Task_MOTOR(void *pvParameters)
{
    TickType_t preTick = xTaskGetTickCount();
    while (1)
    {
        motor_run(motor_speed);
        vTaskDelayUntil(&preTick, MOTOR_TASK_PERIOD_MS);
    }
}

void Motor_Task_KEY(void *pvParameters)
{
    TickType_t preTick = xTaskGetTickCount();
    while (1)
    {
        KEY_TYPE key = key_scan();
        // 电机启动
        if (key == COM_KEY1_DOWN)
        {
            motor_start();
            printf("task_key1 down\r\n");
        }
        // 电机停止
        if (key == COM_KEY2_DOWN)
        {
            motor_stop();
            printf("Task_key2 down off motor\r\n");
        }
        // 正转
        if (key == COM_KEY3_DOWN)
        {
            motor_speed = 900;
            printf("task_key3 down off motor\r\n");
        }
        // 反转
        if (key == COM_KEY4_DOWN)
        {
            motor_speed = 100;
            printf("task_key4 down off motor\r\n");
        }
        vTaskDelayUntil(&preTick, KEY_TASK_PERIOD_MS);
    }
}


extern uint8_t rx_buffer[5];
void Motor_Task_RECEIVE(void *pvParameters)
{
    printf("%s\r\n", rx_buffer);
    printf("3,%c",rx_buffer[0]);
    TickType_t preTick = xTaskGetTickCount();
    while (1)
    {
        if (rx_buffer[0] == '1')
        {
            // 启动
            motor_start();

        }
        else if (rx_buffer[0] == '2')
        {
            // 停止
            motor_stop();
        }
        else if (rx_buffer[0] == '3')
        {
            // 正转
            motor_start();
            motor_speed = 900;
        }
        else if (rx_buffer[0] == '4')
        {
            // 反转
            motor_start();
            motor_speed = 100;
        }
        rx_buffer[0] = '#';
        // switch (buffer[0])
        // {
        // case 1:
        //     /* code */
        //     break;
        
        // default:
        //     break;
        // }
        vTaskDelayUntil(&preTick, RECEIVE_TASK_PERIOD_MS);
    }
}
